﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using System.Drawing;
using NPOI.SS.UserModel;
using IHISCETeim.Model;
using System.Reflection;
using System.IO;
using NPOI.SS.Util;


namespace IHISCETeim.Web.Export.AgentSys
{
    public partial class AgentPWPartSing : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "/Agent/保外配件折扣订货单.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "订货单";
            }
        }
        protected int OrderId
        {
            get
            {
                return GetRequestValue<int>("ID", 0);
            }
        }
        protected AgentPartOrderInfo _objPart = null;
        protected AgentPartOrderInfo objPart
        {
            get
            {
                if (_objPart == null)
                {
                    _objPart = AgentPartOrderController.GetList(OrderId);
                }
                return _objPart;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (OrderId <= 0)
            {
                Response.Write("非法请求");
                return;
            }
            InitializeWorkbook();
            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
           // hssfworkbook.SetSheetName(0, objQlty.ClaimsNumber);

            Type typeUnsu = objPart.GetType();
            PropertyInfo[] propArry = typeUnsu.GetProperties();

            int rowIndex = 0;
            int cellIndex = 0;
            string trueWriteTag = "";
            object provalue = null;
            string PropertyTypeString = "System.String";
            foreach (var item in propArry)
            {
                provalue = item.GetValue(objPart, null);//属性值
                PropertyTypeString = item.PropertyType.ToString();
                //获取特性配置值
                object[] objRowIndex = item.GetCustomAttributes(typeof(ExcelRowIndexAttribute), false);
                if (objRowIndex.Length > 0)//说明该属性使用了ExcelRowIndexAttribute标记
                {
                    rowIndex = (objRowIndex[0] as ExcelRowIndexAttribute).RowIndex;

                }
                else
                {
                    continue;//没有行特性,进入下一个
                }
                object[] objCellIndex = item.GetCustomAttributes(typeof(ExcelCellIndexAttribute), false);
                if (objCellIndex.Length > 0)//说明该属性使用了ExcelRowIndexAttribute标记
                {
                    cellIndex = (objCellIndex[0] as ExcelCellIndexAttribute).CellIndex;
                }

                object[] objBoolCell = item.GetCustomAttributes(typeof(ExcelBoolCellAttribute), false);
                if (objBoolCell.Length > 0)//说明该属性使用了ExcelRowIndexAttribute标记
                {
                    trueWriteTag = (objBoolCell[0] as ExcelBoolCellAttribute).TrueWriteTag;
                    if (Convert.ToBoolean(provalue))
                    {
                        provalue = trueWriteTag;
                    }
                    else
                    {
                        provalue = "";
                    }
                    PropertyTypeString = "System.String";
                }


                ConvertDateTypeHelper.WriteExcelDateType(sheet1.GetRow(rowIndex).GetCell(cellIndex), PropertyTypeString, provalue);
                ////写入值
                //if (provalue == null)
                //    provalue = "";
                // sheet1.GetRow(rowIndex).GetCell(cellIndex).SetCellValue(provalue.ToString());

            }
            //2.写入图片
            InsertImages(sheet1);
          
            //3.写入返回入报废相关列表
           InsertDetail(sheet1);

            sheet1.ForceFormulaRecalculation = true;
            WriteToFile();
        }

        protected void SingExportProc(int QltyId, int SheetIndex)
        {

        }

        #region 加载图片方法
        public void InsertImages(ISheet sheet1)
        {
            HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();
            HSSFClientAnchor anchor;
            int imgRowIndex = 0;//图片所在起始行号
            int imgCellIndex = 0;//图片所在起始列号
            int imgEndRowIndex = 0;//图片所在结束行号
            int imgEndCellIndex = 0;//图片所在结束列号
            string deskpath = @"E:\Web\IHISCE\";
           // string deskpath = @"D:\Work\prj\石川岛中骏\IHISCETeim\IHISCETeim.Web";
            //1.机号
            imgCellIndex = 0; imgRowIndex = 25; imgEndCellIndex = 4; imgEndRowIndex = 26;
            anchor = new HSSFClientAnchor(0, 0, 0, 0, imgCellIndex, imgRowIndex, imgEndCellIndex, imgEndRowIndex);
            anchor.AnchorType = 3;
            try
            {   //加载图片，如果出错，则不管             
                HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(deskpath + objPart.JHImg.Replace("~", ""), hssfworkbook));
                picture.LineStyle = LineStyle.DashSys;
                picture.LineWidth = 12700;
                picture.SetLineStyleColor(0, 0, 0);
            }
            catch
            {

            }
            //2.工时
            imgCellIndex = 5; imgRowIndex = 25; imgEndCellIndex = 8; imgEndRowIndex = 26;
            anchor = new HSSFClientAnchor(0, 0, 0, 0, imgCellIndex, imgRowIndex, imgEndCellIndex, imgEndRowIndex);
            anchor.AnchorType = 3;
            try
            {   //加载图片，如果出错，则不管             
                HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(deskpath + objPart.GSImg.Replace("~", ""), hssfworkbook));
                picture.LineStyle = LineStyle.DashSys;
                picture.LineWidth = 12700;
                picture.SetLineStyleColor(0, 0, 0);
            }
            catch
            {

            }
            //3.更换前照片
            imgCellIndex = 0; imgRowIndex = 27; imgEndCellIndex = 4; imgEndRowIndex = 28;
            anchor = new HSSFClientAnchor(0, 0, 0, 0, imgCellIndex, imgRowIndex, imgEndCellIndex, imgEndRowIndex);
            anchor.AnchorType = 3;
            try
            {   //加载图片，如果出错，则不管             
                HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(deskpath + objPart.ChangeBeforImg.Replace("~", ""), hssfworkbook));
                picture.LineStyle = LineStyle.DashSys;
                picture.LineWidth = 12700;
                picture.SetLineStyleColor(0, 0, 0);
            }
            catch
            {

            }

            //4.更换后照片
            imgCellIndex = 5; imgRowIndex = 27; imgEndCellIndex = 8; imgEndRowIndex = 28;
            anchor = new HSSFClientAnchor(0, 0, 0, 0, imgCellIndex, imgRowIndex, imgEndCellIndex, imgEndRowIndex);
            anchor.AnchorType = 3;
            try
            {   //加载图片，如果出错，则不管             
                HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(deskpath + objPart.ChangeAfertImg.Replace("~", ""), hssfworkbook));
                picture.LineStyle = LineStyle.DashSys;
                picture.LineWidth = 12700;
                picture.SetLineStyleColor(0, 0, 0);
            }
            catch
            {

            }
        }

      
        public int LoadImage(string path, HSSFWorkbook wb)
        {
            string newPitPath = FileUploadHelper.GetSmallPic(path, 235, 170);

            //FileStream file = new FileStream(newPitPath, FileMode.Open, FileAccess.Read);
            //byte[] buffer = new byte[file.Length];
            //file.Read(buffer, 0, (int)file.Length);
            //return wb.AddPicture(buffer, PictureType.JPEG);

            using (FileStream file = new FileStream(newPitPath, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[file.Length];
                file.Read(buffer, 0, (int)file.Length);
                return wb.AddPicture(buffer, PictureType.JPEG);
            }
        }

        public void InsertDetail(ISheet sheet1)
        {
            List<AgentPartOrderDetailInfo> listParts = AgentPartOrderController.GetAllDetailByOrderId(OrderId);
            int baseFHIndex = 8;
            foreach (AgentPartOrderDetailInfo item in listParts)
            {
                //sheet1.GetRow(baseFHIndex).ZeroHeight = false;
                sheet1.GetRow(baseFHIndex).GetCell(1).SetCellValue(item.ItemCode);
                sheet1.GetRow(baseFHIndex).GetCell(2).SetCellValue(item.Dscription);
                sheet1.GetRow(baseFHIndex).GetCell(3).SetCellValue(Convert.ToDouble(item.Quantity));

                sheet1.GetRow(baseFHIndex).GetCell(4).SetCellValue(Convert.ToDouble(item.OldPrice));
                sheet1.GetRow(baseFHIndex).GetCell(5).SetCellValue(Convert.ToDouble(item.Price));
                sheet1.GetRow(baseFHIndex).GetCell(6).SetCellValue(Convert.ToDouble(item.LineTotal));
                sheet1.GetRow(baseFHIndex).GetCell(7).SetCellValue(GetBuyType(item.BuyType));
                sheet1.GetRow(baseFHIndex).GetCell(8).SetCellValue(item.ShipType);
               // sheet1.GetRow(baseFHIndex).GetCell(9).SetCellValue(item.LineRemark);
                baseFHIndex++;
            }

        }

        #endregion

        private string GetBuyType(string oldBuyType)
        {
            switch (oldBuyType)
            {
                case "库存订货":
                    return "库存订货(8.5折)";
                    break;
                case "紧急订货":
                    return "紧急订货(9折)";
                    break;
            }
            return oldBuyType;
        }
        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}{1}.xls", "保外配件订单", objPart.OrderNumber).Trim();
        }
    }
}